﻿using System;
using System.Collections.Generic;
using System.Text;
using Cashier_Model;
using System.Collections;
using System.Drawing.Printing;
using Cashier.utils;
using System.Drawing;
using Cashier_Barcode;
using Cashier_Service;

namespace Cashier.prints
{
    /// <summary>
    /// POS小票打印对象
    /// </summary>
    public class PosBillPrintDoc
    {
        private BillShipment billShipment;
        private ArrayList goodsList;
        private Member member;
        private MemberService memberService;

        public PosBillPrintDoc(BillShipment billShipment, ArrayList goodsList)
        {
            this.billShipment = billShipment;
            this.goodsList = goodsList;
            this.memberService = new MemberService();

            if (!String.IsNullOrEmpty(billShipment.Memberno))
                member = memberService.GetMember(0, billShipment.Memberno);
        }

        public BillShipment BillShipment
        {
            get { return billShipment; }
            set { billShipment = value; }
        }       

        public ArrayList GoodsList
        {
            get { return goodsList; }
            set { goodsList = value; }
        }

        public DateTime PrintTime
        {
            get { return DateTime.Now; }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="shipmentno">出货单号</param>
        /// <param name="username">收银员ID</param>
        /// <param name="e">打印对象</param>
        public void Print(String shipmentno, String username, PrintPageEventArgs e)
        {
            try
            {
                if (this.billShipment != null && this.goodsList != null && this.goodsList.Count > 0)
                {
                    //BarcodeFactory.DrawBarcode128(e.Graphics, 0, 0, false, String.Empty, shipmentno, 0, 40);
                    int rectWidth = 185; //矩形宽
                    int lineHeight = 14; //矩形高
                    //定义顶点的坐标
                    int x = 0, y = 0;

                    //定义矩形画笔
                    Pen rectPen = new Pen(Brushes.White, 1);
                    rectPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;

                    //定义打印字体
                    Font font = new Font(new FontFamily("宋体"), 8);
                    StringFormatStyle sfs = new StringFormatStyle();

                    //定义分隔线
                    Pen splitLinePen = new Pen(Brushes.Black);
                    splitLinePen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;


                    //画矩形
                    Rectangle rect = new Rectangle(x, y, rectWidth, lineHeight);
                    //e.Graphics.FillRectangle(Brushes.White, rect);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    String s = "===" + Config.SHOP_NAME + "===";
                    e.Graphics.DrawString(s, font, Brushes.Black, rect, sfs.CenterMiddle);

                    //打印分隔线
                    e.Graphics.DrawLine(splitLinePen, x, rect.Y + rect.Height + 2, x + rect.Width, rect.Y + rect.Height + 2);

                    if (!String.IsNullOrEmpty(billShipment.StoreNo))
                    {
                        //打印票头信息
                        rect = new Rectangle(x, rect.Y + lineHeight + 6, rectWidth, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        s = "门店号：" + billShipment.StoreNo;
                        e.Graphics.DrawString(s, font, Brushes.Black, rect, sfs.LeftMiddle);
                    }
                    else
                        rect.Y += 6;

                    rect = new Rectangle(x, rect.Y + lineHeight, rectWidth, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    s = "打印时间：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    e.Graphics.DrawString(s, font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x, rect.Y + lineHeight, rectWidth, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    s = "收银员：" + username;
                    e.Graphics.DrawString(s, font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x, rect.Y + lineHeight, rectWidth, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    s = "出货单号：" + shipmentno;
                    e.Graphics.DrawString(s, font, Brushes.Black, rect, sfs.LeftMiddle);

                    //打印分隔线
                    e.Graphics.DrawLine(splitLinePen, x, rect.Y + rect.Height + 2, x + rect.Width, rect.Y + rect.Height + 2);

                    //画表头
                    rect = new Rectangle(x, rect.Y + rect.Height + 6, 50, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("商品名称", font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(rect.X + rect.Width, rect.Y, 50, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("单价", font, Brushes.Black, rect, sfs.RightMiddle);

                    rect = new Rectangle(rect.X + rect.Width, rect.Y, 35, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("数量", font, Brushes.Black, rect, sfs.RightMiddle);

                    rect = new Rectangle(rect.X + rect.Width, rect.Y, 50, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("金额", font, Brushes.Black, rect, sfs.RightMiddle);

                    //画商品
                    int tempX = x;
                    int tempY = rect.Y + lineHeight;
                    for (int i = 0; i < goodsList.Count; i++)
                    {
                        BillShipmentGoods goods = (BillShipmentGoods)goodsList[i];
                        rect = new Rectangle(tempX, tempY, rectWidth, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(goods.GoodsName, font, Brushes.Black, rect, sfs.LeftMiddle);

                        rect = new Rectangle(x, rect.Y + rect.Height, 50, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString("", font, Brushes.Black, rect, sfs.LeftMiddle);

                        rect = new Rectangle(rect.X + rect.Width, rect.Y, 50, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(goods.PriceDisc.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                        rect = new Rectangle(rect.X + rect.Width, rect.Y, 35, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(goods.Numbers.ToString(), font, Brushes.Black, rect, sfs.RightMiddle);

                        rect = new Rectangle(rect.X + rect.Width, rect.Y, 50, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(goods.Total.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                        tempY = rect.Y + lineHeight;
                    }

                    //打印分隔线
                    e.Graphics.DrawLine(splitLinePen, x, rect.Y + rect.Height + 2, x + rectWidth, rect.Y + rect.Height + 2);

                    //打印票尾信息
                    rect = new Rectangle(x, rect.Y + rect.Height + 6, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("商品总金额：", font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString(billShipment.GoodsTotal.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                    if (member != null && billShipment.IntegralAmount > 0)
                    {
                        rect = new Rectangle(x, rect.Y + rect.Height + 6, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString("积分支付：", font, Brushes.Black, rect, sfs.LeftMiddle);

                        rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(billShipment.IntegralAmount.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);
                    }

                    if (member!=null && billShipment.MemberBalance > 0)
                    {
                        rect = new Rectangle(x, rect.Y + rect.Height + 6, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString("会员卡支付：", font, Brushes.Black, rect, sfs.LeftMiddle);

                        rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(billShipment.MemberBalance.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);
                    }

                    rect = new Rectangle(x, rect.Y + rect.Height + 6, rectWidth/2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("应付金额：", font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString(billShipment.PayableAmount.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                    /*
                    rect = new Rectangle(x, rect.Y + rect.Height, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("优惠金额：", font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString(billShipment.DiscountTotal.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);
                    */

                    //打印分隔线
                    e.Graphics.DrawLine(splitLinePen, x, rect.Y + rect.Height + 2, x + rectWidth, rect.Y + rect.Height + 2);

                    rect = new Rectangle(x, rect.Y + rect.Height + 5, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("实付金额：", font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString(billShipment.PayAmount.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                    rect = new Rectangle(x, rect.Y + rect.Height, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString("找零金额：", font, Brushes.Black, rect, sfs.LeftMiddle);

                    rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                    e.Graphics.DrawRectangle(rectPen, rect);
                    e.Graphics.DrawString(billShipment.GiveAmount.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                    //打印分隔线
                    e.Graphics.DrawLine(splitLinePen, x, rect.Y + rect.Height + 2, x + rectWidth, rect.Y + rect.Height + 2);

                    int addRowHeight = 10;
                    if (member != null)
                    {
                        rect = new Rectangle(x, rect.Y + rect.Height + 5, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString("会员卡号：", font, Brushes.Black, rect, sfs.LeftMiddle);

                        rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(member.Memberno, font, Brushes.Black, rect, sfs.RightMiddle);

                        rect = new Rectangle(x, rect.Y + rect.Height, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString("卡内余额：", font, Brushes.Black, rect, sfs.LeftMiddle);

                        rect = new Rectangle(x + rect.Width, rect.Y, rectWidth / 2, lineHeight);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(member.Balance.ToString("f2"), font, Brushes.Black, rect, sfs.RightMiddle);

                        //addRowHeight = lineHeight * 2;
                    }

                    String ticketPromotions = Config.TICKET_PROMOTIONS;
                    if (!String.IsNullOrEmpty(ticketPromotions) && ticketPromotions.Length > 0)
                    {
                        int pRows = ticketPromotions.Length / 15;
                        if (ticketPromotions.Length % 15 > 0)
                            pRows += 1;
                        rect = new Rectangle(x, rect.Y + rect.Height + addRowHeight, rectWidth, lineHeight * pRows);
                        e.Graphics.DrawRectangle(rectPen, rect);
                        e.Graphics.DrawString(ticketPromotions, font, Brushes.Black, rect, sfs.LeftMiddle);
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }
    }
}
